package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzShijuanMapper;
import com.ruoyi.system.domain.DzShijuan;
import com.ruoyi.system.service.IDzShijuanService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 试卷信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@Service
public class DzShijuanServiceImpl implements IDzShijuanService 
{
    @Autowired
    private DzShijuanMapper dzShijuanMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询试卷信息
     * 
     * @param sjId 试卷信息主键
     * @return 试卷信息
     */
    @Override
    public DzShijuan selectDzShijuanBySjId(Long sjId)
    {
        return dzShijuanMapper.selectDzShijuanBySjId(sjId);
    }

    /**
     * 查询试卷信息列表
     * 
     * @param dzShijuan 试卷信息
     * @return 试卷信息
     */
    @Override
    public List<DzShijuan> selectDzShijuanList(DzShijuan dzShijuan)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzShijuan.setDatascope(datascope);
        if(datascope==4){
            dzShijuan.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzShijuan.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzShijuan.setCheckHuiyuanId(0l);
            }

        }
        return dzShijuanMapper.selectDzShijuanList(dzShijuan);
    }

    @Override
    public List<DzShijuan> selectDzShijuanListWeb(DzShijuan dzShijuan)
    {
        return dzShijuanMapper.selectDzShijuanList(dzShijuan);
    }

    /**
     * 新增试卷信息
     * 
     * @param dzShijuan 试卷信息
     * @return 结果
     */
    @Override
    public int insertDzShijuan(DzShijuan dzShijuan)
    {
        return dzShijuanMapper.insertDzShijuan(dzShijuan);
    }

    /**
     * 修改试卷信息
     * 
     * @param dzShijuan 试卷信息
     * @return 结果
     */
    @Override
    public int updateDzShijuan(DzShijuan dzShijuan)
    {
        return dzShijuanMapper.updateDzShijuan(dzShijuan);
    }

    /**
     * 批量删除试卷信息
     * 
     * @param sjIds 需要删除的试卷信息主键
     * @return 结果
     */
    @Override
    public int deleteDzShijuanBySjIds(Long[] sjIds)
    {
        return dzShijuanMapper.deleteDzShijuanBySjIds(sjIds);
    }

    /**
     * 删除试卷信息信息
     * 
     * @param sjId 试卷信息主键
     * @return 结果
     */
    @Override
    public int deleteDzShijuanBySjId(Long sjId)
    {
        return dzShijuanMapper.deleteDzShijuanBySjId(sjId);
    }
}
